const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const dotenv = require('dotenv');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');

const env = process.env.NODE_ENV || 'production';
dotenv.config({ path: '.env' + env});
dotenv.config();

const envs = Object.keys(process.env).reduce((e, i) => {
  if (i.startsWith('MY_')) {
    e[i] = JSON.stringify(process.env[i]);
  }
  return e;
}, {});

/**
 * @type {import('webpack').Configuration}
 */
module.exports = {
  mode: 'none',
  entry: './src/main.js',
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'js/bundle.[contenthash:6].js'
  },
  module: {
    rules: [
      {
        test: /\.less$/,
        use: ['style-loader', 'css-loader', 'less-loader'],
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.vue$/,
        use: ['vue-loader', 'eslint-loader'],
      },
      {
        test: /\.(png|jpe?g|gif)$/,
        use: {
          loader: 'url-loader',
          options: {
            limit: 3 *1024,
            esModule: false,
            name: 'img/[name].[contenthash:6].ext',
          },
        },
      }
    ],
  },
  plugins: [
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      template: './public/index.html',
      title: 'Demo APP',
    }),
    new webpack.DefinePlugin({
      BASE_URL: JSON.stringify(envs.MY_BASE_URL),
    })
  ]
};